package win.smartown.app.base;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;
import win.smartown.app.request.RequestListener;
import win.smartown.app.util.SnackbarUtil;

/**
 * Created by smartown on 2017/3/18 17:11.
 * Desc:
 */
public class SuperActivity extends AppCompatActivity {

    private CompositeSubscription compositeSubscription;
    private Snackbar snackbar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        compositeSubscription = new CompositeSubscription();
    }

    @Override
    protected void onDestroy() {
        unSubscribe();
        super.onDestroy();
    }

    public void addSubscription(Subscription subscription) {
        if (compositeSubscription == null) {
            compositeSubscription = new CompositeSubscription();
        }
        compositeSubscription.add(subscription);
    }

    public void unSubscribe() {
        if (compositeSubscription != null) compositeSubscription.unsubscribe();
    }

    public <T> void request(Observable<T> observable, final RequestListener<T> listener) {
        addSubscription(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<T>() {

                    @Override
                    public void onStart() {
                        listener.onStart();
                    }

                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        listener.onFail(e);
                    }

                    @Override
                    public void onNext(T t) {
                        listener.onSuccess(t);
                    }
                }));
    }

    protected <T extends View> T findView(@IdRes int id) {
        return (T) findViewById(id);
    }

    protected void showSnackBar(CharSequence text) {
        showSnackBar(text, 3000);
    }

    protected void showSnackBar(CharSequence text, int duration) {
        showSnackBar(text, duration, null, null);
    }

    protected void showSnackBar(CharSequence text, CharSequence action, View.OnClickListener actionListener) {
        showSnackBar(text, 3000, action, actionListener);
    }

    protected void showSnackBar(CharSequence text, int duration, CharSequence action, View.OnClickListener actionListener) {
        if (snackbar == null) {
            snackbar = setupSnackbar();
        }
        snackbar.setText(text);
        snackbar.setDuration(duration);
        if (!TextUtils.isEmpty(action) && null != actionListener) {
            snackbar.setAction(action, actionListener);
        }
        snackbar.show();
    }

    protected Snackbar setupSnackbar() {
        return SnackbarUtil.newSnackbar(getWindow().getDecorView());
    }

}
